﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net;
using System.Text;
using System.Threading.Tasks;
using BrnMall.Sdk.WeiXin.Tool;
using BrnMall.Sdk.WeiXin.weapp;
using BrnMall.WeiXin.Sdk;
using Newtonsoft.Json;

namespace BrnMall.Sdk.WeiXin
{
    public static  class WeAppSdk
    {
        /// <summary>
        /// 操作域名配置
        /// </summary>
        /// <param name="action"></param>
        /// <param name="access_token"></param>
        /// <param name="domain">不带http https前缀</param>
        public static BaseResult SetDomain(string access_token, string domain)
        {
            var url = $"https://api.weixin.qq.com/wxa/modify_domain?access_token={access_token}";
            var postData=$"{{\"action\":\"set\",\"requestdomain\":[\"https://{domain}\",\"https://webim.tim.qq.com\",\"https://static.711688.net.cn\"],\"wsrequestdomain\":[\"wss://{domain}\"],\"uploaddomain\":[\"https://{domain}\"],\"downloaddomain\":[\"https://{domain}\",\"https://static.711688.net.cn\"]}}";
           return RestHttp.Post<BaseResult>(url, postData);
        }
        /// <summary>
        /// 绑定体验者
        /// </summary>
        /// <param name="access_token"></param>
        /// <param name="wechatid"></param>
        /// <returns></returns>
        public static BaseResult BindTester(string access_token, string wechatid)
        {
            var url = $"https://api.weixin.qq.com/wxa/bind_tester?access_token={access_token}";
            var postData = $"{{\"wechatid\":\"{wechatid}\"}}";
            return RestHttp.Post<BaseResult>(url, postData);
        }

        /// <summary>
        /// 为授权的小程序帐号上传小程序代码
        /// </summary>
        /// <param name="accessToken"></param>
        /// <param name="data"></param>
        /// <returns></returns>
        public static BaseResult Commit(string accessToken, CommitPostData data)
        {
            var url = $"https://api.weixin.qq.com/wxa/commit?access_token={accessToken}";
            var postData = JsonConvert.SerializeObject(data);
            LogUtil.WriteLog(postData);
            return RestHttp.Post<BaseResult>(url, postData);
        }
        /// <summary>
        /// 获取体验小程序的体验二维码
        /// </summary>
        /// <param name="accessToken"></param>
        /// <returns></returns>
        public static string GetQrCodeUrl(string accessToken)
        {
            var url= $"https://api.weixin.qq.com/wxa/get_qrcode?access_token={accessToken}";
            var request = (HttpWebRequest)WebRequest.Create(url);
            request.Method = "GET";
            request.ContentType = "application/x-www-form-urlencoded";
            var response = (HttpWebResponse)request.GetResponse();
            var resStream = response.GetResponseStream();
            if (resStream == null) return string.Empty;
            var dirPath =WeiXinSdk.GetMapPath("/upload/user/");
            var newFileName = $"acode_{DateTime.Now:yyMMddHHmmssfffffff}.jpg";
            var savePath = dirPath + newFileName;
            if (!Directory.Exists(dirPath))
                Directory.CreateDirectory(dirPath);
            using (var fs = new FileStream(savePath, FileMode.Create, FileAccess.Write, FileShare.ReadWrite))
            {
                byte[] buff = new byte[512];
                int c = 0; //实际读取的字节数
                while ((c = resStream.Read(buff, 0, buff.Length)) > 0)
                {
                    fs.Write(buff, 0, c);
                }
            }
            return savePath;
        }
        /// <summary>
        /// 获取分类
        /// </summary>
        /// <param name="accessToken"></param>
        /// <returns></returns>
        public static CategoryResponse GetCategory(string accessToken)
        {
            var url = $"https://api.weixin.qq.com/wxa/get_category?access_token={accessToken}";
            return RestHttp.Get<CategoryResponse>(url);
        }

        /// <summary>
        /// 提交审核
        /// </summary>
        /// <param name="accessToken"></param>
        /// <param name="data"></param>
        /// <returns></returns>
        public static SubmitAuditResponse SubmitAudit(string accessToken, SubmitAudit data)
        {
            var url = $"https://api.weixin.qq.com/wxa/submit_audit?access_token={accessToken}";
            var postData = JsonConvert.SerializeObject(data);
            LogUtil.WriteLog(postData);
            return RestHttp.Post<SubmitAuditResponse>(url, postData);
        }

        public static GetAuditStatusResponse GetAuditStatus(string accessToken, long auditid)
        {
            var url = $"https://api.weixin.qq.com/wxa/get_auditstatus?access_token={accessToken}";
            var postData = $"{{\"auditid\":{auditid}}}";
            return RestHttp.Post<GetAuditStatusResponse>(url, postData);
        }
        public static GetLastAuditStatusResponse GetLastAuditStatus(string accessToken)
        {
            var url = $"https://api.weixin.qq.com/wxa/get_latest_auditstatus?access_token={accessToken}";
            return RestHttp.Get<GetLastAuditStatusResponse>(url);
        }

        /// <summary>
        /// 发布上线
        /// </summary>
        /// <param name="accessToken"></param>
        /// <returns></returns>
        public static BaseResult Release(string accessToken)
        {
            var url = $"https://api.weixin.qq.com/wxa/release?access_token={accessToken}";
            const string postData = "{}";
            return RestHttp.Post<BaseResult>(url, postData);
        }

        public static BaseResult DeleteTemplate(string accessToken, int templateId)
        {
            var url = $"https://api.weixin.qq.com/wxa/deletetemplate?access_token={accessToken}";
             var postData = $"{{\"template_id\":{templateId}}}";
            return RestHttp.Post<BaseResult>(url, postData);
        }

        public static Pages GetTempatePages(string accessToken)
        {
            var url = $"https://api.weixin.qq.com/wxa/get_page?access_token={accessToken}";
            return RestHttp.Get<Pages>(url);
        }
    }

}
